import re

ret = re.search(r'^ab','abcdefg')
"<re.Match object; span=(0, 2), match='ab'>"

# $ 匹配字符串\n结尾
ret = re.search(r'foo$','foo1\nfoo2\n')
'None'
ret = re.search(r'foo$','foobar')
'None'
ret = re.search(r'foo$','foo\n')
"<re.Match object; span=(0, 3), match='foo'>"

# * 匹配前一个字符出现0次或多次
# ab* 匹配 a, ab, abb ; b可以出现0次或多次
ret = re.search(r'ab*','abc')
"<re.Match object; span=(0, 2), match='ab'>"
ret = re.search(r'ab*','a')
"<re.Match object; span=(0, 1), match='a'>"

# + 匹配前一个字符出现1次或多次
# ab+ 匹配 ab, abb ; b可以出现1次或多次
ret = re.search(r'ab+','abc')
"<re.Match object; span=(0, 2), match='ab'>"
ret = re.search(r'ab+','a')
"None"
ret = re.search(r'ab+','abbc')
"<re.Match object; span=(0, 3), match='abb'>"

# ? 匹配前一个字符出现0次或1次
# ab? 匹配 a, ab
ret = re.search(r'ab?','abc')
"<re.Match object; span=(0, 2), match='ab'>"
ret = re.search(r'ab?','ac')
"<re.Match object; span=(0, 1), match='a'>"

# {m} 匹配前一个字符出现m次
# a{2} 匹配 aa, a{3} 匹配 aaa
ret = re.search(r'a{2}','aabefg')
"<re.Match object; span=(0, 2), match='aa'>"

# {m,n} 匹配前一个字符出现m到n次
# a{2,3} 匹配 aa, aaa
ret = re.search(r'a{2,3}','aabefg')
"<re.Match object; span=(0, 2), match='aa'>"
ret = re.search(r'a{2,3}','aaabefg')
"<re.Match object; span=(0, 3), match='aaa'>"
ret = re.search(r'a{2,3}','aaaabefg')
"<re.Match object; span=(0, 3), match='aaa'>"

# {m,n}? 非贪婪模式，尽可能少的匹配
# a{2,3}? 匹配 aa
ret = re.search(r'a{2,3}?','aaabefg')
"<re.Match object; span=(0, 2), match='aa'>"
print(ret)
